
#ifndef GRAPH_H
#define GRAPH_H

#include <QPainter>
#include <QAbstractItemModel>
#include <QTime>
#include "grid.h"
#include "plot.h"
#include "marker.h"
#include "zoombox.h"
#include "measurebox.h"
#include "levelmarker.h"

class Graph : public QWidget
{
    Q_OBJECT
	Q_PROPERTY(QString Title READ getTitle WRITE setTitle)
	Q_PROPERTY(QString XAxis READ getXAxis WRITE setXAxis)
	Q_PROPERTY(QString YAxis READ getYAxis WRITE setYAxis)
	Q_PROPERTY(int marginTop READ getMarginTop WRITE setMarginTop)
	Q_PROPERTY(int marginBottom READ getMarginBottom WRITE setMarginBottom)
	Q_PROPERTY(int marginRight READ getMarginRight WRITE setMarginRight)
	Q_PROPERTY(int marginLeft READ getMarginLeft WRITE setMarginLeft)
	Q_PROPERTY(int gridIncrementY READ getGridIncrementY WRITE setGridIncrementY)
	Q_PROPERTY(int gridIncrementX READ getGridIncrementX WRITE setGridIncrementX)

	Q_PROPERTY(QColor gridColor READ getGridColor WRITE setGridColor)
	
//	Q_PROPERTY(GridType gridType READ getGridType WRITE setGridType)
	Q_ENUMS(GridType)

public:
    Graph(QWidget *parent = 0);
	~Graph();
    
	QRect getGridArea();
	void setModel(QAbstractItemModel *newModel);
	void setData(double* data, int size, const QString& name = "Ref");
	bool createPlot(const QString& plotName, int size);
	bool deletePlot(const QString& plotName);
	int	 totalPlots();
	enum GridType { GRID, GRID2 };

    void enablePlot(bool);

	/*
	**  Label properties
	*/
	void setTitle(QString s);
	void setXAxis(QString s);
	void setYAxis(QString s);

	QString getTitle();
	QString getXAxis();
	QString getYAxis();

	/*
	**  Set and get the margins bounding the graphing area.
	*/
	void setMarginTop(int m);
	void setMarginBottom(int m);
	void setMarginLeft(int m);
	void setMarginRight(int m);

	int getMarginTop();
	int getMarginBottom();
	int getMarginLeft();
	int getMarginRight();

	int getGridIncrementX();
	int getGridIncrementY();

	void setGridIncrementX(int num);
	void setGridIncrementY(int num);

	QList<Plot*> getPlotList();
	QList<Marker *>	getMarkerList();
    LevelMarker * getLevelMarker();
	
	int	getBaseLine();	

	void setXRange(const double& lower, const double& upper);
	void setYRange(const double& lower, const double& upper);

	void setGridList(QString q);
	QString getGridList();

	QColor getGridColor();
	void setGridColor(QColor color);

	QColor getPlotColor(const QString& plotName = "Ref");
	void setPlotColor(QColor color, const QString& plotName = "Ref");

	QColor getM1Color();
	void setM1Color(QColor color);

	QColor getMcColor();
	void setMcColor(QColor color);

	QColor getM2Color();
	void setM2Color(QColor color);

	QColor getBgColor();
	void setBgColor(QColor color);

	QColor getZoomColor();
	void setZoomColor(QColor color);

	QColor getZoomOutlineColor();
	void setZoomOutlineColor(QColor color);

	Qt::BrushStyle getZoomStyle();
	void setZoomStyle(Qt::BrushStyle);

	void setTitleFont(QFont titleFont);
	QFont getTitleFont();

	void setValuesFont(QFont valuesFont);
	QFont getValuesFont();

	void setAxisLabelsFont(QFont axisLabelFont);
	QFont getAxisLabelsFont();

	void setMarkersFont(QFont font);
	QFont getMarkersFont();

	GridType getGridType();

	/* Marker Movement */
	void moveMarker(int markerId, double position);
	void addMarkers(int numMarkers);

	void setMarkerLevelAlarm(bool enabled);
	void resetAlarms();

    void setDrawGrid(bool toggle);

    void setZoomXAxis(bool toggled);
    void setZoomYAxis(bool toggled);
signals:
	/* Zoom Signals */
	void zoomChangedX(int minRange, int maxRange, int minCurrent, int maxCurrent);
	void zoomChangedY(int minRange, int maxRange, int minCurrent, int maxCurrent);

	/* Marker Signals */
	void markerMoved(int markerId, double position, double value);
	void markerLevelAlarmTriggered();


private slots:

	/* Painting and toggle Slots */
    void paintEvent(QPaintEvent *pe);
    void timerEvent(QTimerEvent *e);

	/* Zooming and Scaling Slots */
	void setZoomLowerX(int index);
	void setZoomLowerY(int index);
	void setBaseLine(int pos);

protected:
	void resizeEvent(QResizeEvent *event);
	void mouseMoveEvent(QMouseEvent *event);
	void mousePressEvent(QMouseEvent *event);
	void mouseReleaseEvent(QMouseEvent *event);
	void keyPressEvent ( QKeyEvent * event );

private:
    void drawGraph(QPainter *p); 
	void updatePlotArea();
	void updateGridLabels();
	void attachMarker(int marker, int plotIndex);

	Grid	*grid;
	int		referencePlotIndex;
    LevelMarker*    mLevelMarker;
    bool            mLevelMarkerActive;
	Zoombox	zoomBox;
    MeasureBox measureBox;
	QList<Plot*> plotList;
	QList<Marker *>	markerList;

    bool    mEnabled;
	int		activeMarker;
	int		marginTop;
	int		marginBottom;
	int		marginRight;
	int		marginLeft;
	int		gridIncrementX;
	int		gridIncrementY;
    double    *dataBuffer;
	int		dataBufferSize;

    QPainter mPainter;
	QPixmap  pixmap;
	QColor	backgroundColor;

	QString list;

	GridType	gridType;
    unsigned int mCount;
	int		markerTimer;

	
	bool zooming;
	bool measuring;
    bool measured;
    bool mZoomXAxis;
    bool mZoomYAxis;
    bool mDrawGrid;
    int receivingData;
    int curAngle;

	QAbstractItemModel *model;

	int mMarkerLevelAlarmValue;
	int mMarkerLevelAlarmEnable;
};

#endif
